package com.probiz.estore.common.model.system;

import java.util.Set;

import org.acegisecurity.GrantedAuthority;
import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.model.system.base.AppMenuTbl;

/**
 * Model class for AppMenu. Add not database mapped fileds in this class.
 */
public class AppMenu extends AppMenuTbl {

  	public static Integer	SORTORDER_DEFAULT_VALUE = 999;

	/**
	 * Default Empty Constructor for class AppMenu
	 */
	public AppMenu () {
		super();
	}
	/**
	 * 定义实体的业务名取值； appMenuName
	 * 必须手工完成这个部分，否则编译不通过。
	 */
	public String getAppMenuName () {
		if (this.menuId == null)
	        return "";
	    else
			//返回一个指定有业务意义的属性值;
			//如：product的VO就用product.productName
	        return this.menuName;
	}
	/**
	 * Default Key Fields Constructor for class AppMenu
	 */
	public AppMenu (
		 Integer in_menuId
		) {
		super (
		  in_menuId
		);
	}

	
	public Set<GrantedAuthority> getAuthorities() {
		return getAppRoles();
		//return (GrantedAuthority[]) appRoles.toArray(new GrantedAuthority[appRoles.size()]);
	}

	/* implement the resourcedetail interface.
	 * 
	 */
	public String getResString() {
		return getUrl();
	}
	
	public String getMenuPath_(){
		if(getMenuPath()==null){
			return getMenuId()+".";
		}else
			return getMenuPath()+getMenuId()+".";
	}
	
	public String getMenuPath__(){
		if(getParent()==null){
			return null;
		}else{
			return getParent().getMenuPath_();
		}
	}
	
	public int getDepth(){
		if(StringUtils.isBlank(menuPath)||menuId==null)
			return 0;
		return menuPath.split("\\.").length+1;
	}
	
	public boolean isThreeLevel(){
		if(StringUtils.isBlank(menuPath)||menuId==null)
			return false;
		return menuPath.split("\\.").length>=2;
	}
	
	public String getMenuNamePath(){
		if(parent==null)
			return null;
		else if(parent.getParent()!=null)
			return " > "+parent.getParent().getMenuName()+" > "+parent.getMenuName();
		else
			return " > "+parent.getMenuName();
		
	}
	/**
	 * 获得多个url资源集合
	 * @return
	 */
	public String[] getUrlResources(){
		if(StringUtils.isNotBlank(this.url)){
			String[] urls = this.url.replaceAll("；", ";").split(";");
			return urls;
		}
		return null;
	}
}
